---
description: "Reference for Nitric's Dart library - Register a handler for HTTP PUT requests to the route."
---

# Dart - api.route.put()

<Note>
  This is reference documentation for the Nitric Dart SDK. To learn about APIs
  in Nitric start with the [API docs](/apis).
</Note>

Register a handler for HTTP PUT requests to the route.

```dart
import 'package:nitric_sdk/nitric.dart';

final customersRoute = Nitric.api("public").route("/customers");

customersRoute.put((ctx) async {
  // construct response for the PUT: /customers request...
  final responseBody = {};
  ctx.res.json(responseBody);

  return ctx;
});
```

## Parameters

<Properties>
  <Property name="handler" required type="HttpHandler">
    The middleware service to use as the handler for HTTP requests.
  </Property>
</Properties>

## Examples

### Register a handler for PUT requests

```dart
import 'package:nitric_sdk/nitric.dart';

final customersRoute = Nitric.api("public").route("/customers");

customersRoute.put((ctx) async {
  // construct response for the PUT: /customers request...
  final responseBody = {};
  ctx.res.json(responseBody);

  return ctx;
});
```

### Chain services as a single method handler

When multiple services are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers. You can call the next middleware in the chain using `ctx.next()`. If a middleware in the chain does not call `.next` and instead returns the base context, the call chain will end.

```dart
import 'package:nitric_sdk/nitric.dart';

Future<HttpContext> validate(HttpContext ctx) async {
  if (!ctx.req.headers.containsKey("Content-Type")) {
    ctx.res.status = 400;
    ctx.res.body = "header Content-Type is required";

    // End the middleware chain by not calling `ctx.next()`.
    return ctx;
  }

  return ctx.next();
}

Future<HttpContext> createOrUpdateCustomer(HttpContext ctx) async {
  // handle the PUT request...
return ctx.next();
}

// The validate middleware will run before all handlers that are created using this route
final customersRoute = Nitric.api("public").route("/customers", middlewares: [validate]);

// The validate middleware will run before the createOrUpdateCustomer handler
customersRoute.put(createOrUpdateCustomer);
```

### Access the request body

The PUT request body is accessible from the `ctx.req` object.

```dart
import 'package:nitric_sdk/nitric.dart';

final customersRoute = Nitric.api("public").route("/customers");

customersRoute.put((ctx) async {
  final customerData = ctx.req.json();
  // parase, validate and store the request payload if it's available

  return ctx;
});
```
